package org.lib.jpamysql;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class JpaMysqlApplication {

	private static Consumer<Object> cs = System.out::println;

	public static void main(String[] args) {
		ConfigurableApplicationContext ctx = SpringApplication.run(JpaMysqlApplication.class, args);

		// 调试用
//		studyContext(ctx);
	}
	
	/**
	 * 分析容器中的Bean
	 * @author ben
	 * @date 2021-10-22 16:59:39 CST
	 * @param ctx
	 */
	public static void studyContext(ConfigurableApplicationContext ctx) {
		cs.accept("\n++++++studyContext++++++\nBeanDefinitionCount=" + ctx.getBeanDefinitionCount());
		String[] beanNames = ctx.getBeanDefinitionNames();
		cs.accept("beanNames.length=" + beanNames.length);
		cs.accept("输出Bean的名称：");
		Arrays.stream(beanNames).forEach((name)->{
			cs.accept("name=" + name);
		});
		
		cs.accept("---");
//		List<String> errBeans = new ArrayList<>();
//		Arrays.stream(beanNames).forEach((name)->{
//			try {
//				cs.accept(String.format("name=%s, bean=%s", name, ctx.getBean(name).toString()));
//			} catch (Exception e) {
//				cs.accept("异常：name=" + name + ", e=" + e.getClass() + ", e.msg=" + e.getMessage());
//				errBeans.add(name);
//			}
//		});
//		cs.accept("errBeans=" + errBeans);
		
		cs.accept("------studyContext------\n");
	}

}
